﻿using DK.Baekdorf.MathTexRender;
using Microsoft.Graphics.Canvas.Text;
using Microsoft.Graphics.Canvas.UI.Xaml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace MathExpressionRender
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private TexFormulaParser formulaParser;
        private TexFormula formula = null;
        private TexRenderer renderer = null;

        public MainPage()
        {
            this.InitializeComponent();

            // Init MathExpressionRender stuff 
            TexFormulaParser.Initialize();
            this.formulaParser = new TexFormulaParser();

            // Sample
            formula = this.formulaParser.Parse(@"\sqrt{34+\pi}");
            renderer = formula.GetRenderer(TexStyle.Display, 120);
        }

        private void ShowSample(string sourceText)
        {
            try
            {
                formula = this.formulaParser.Parse(sourceText);
                renderer = formula.GetRenderer(TexStyle.Display, sizeSlider.Value);
                sourceTextBlock.Text = string.Format("Source: {0}", sourceText); ;
                myCanvas.Invalidate();
            }
            catch (Exception ex)
            {
                sourceTextBlock.Text = string.Format("Error:",  ex.Message);
            }

        }

        private void OnDraw(CanvasControl sender, CanvasDrawEventArgs args)
        {
            if (renderer != null)
            {
                renderer.Render(args.DrawingSession, 0, 0);
            }
        }

        private void Sample1(object sender, RoutedEventArgs e)
        {
            ShowSample(@"\sqrt{34+\pi}");
        }

        private void Sample2(object sender, RoutedEventArgs e)
        {
            ShowSample(@"\sqrt{\frac{7}{15}}");
        }

        private void Sample3(object sender, RoutedEventArgs e)
        {
            ShowSample(@"L=\int_a^b\gamma(t)");
        }

        private void Sample4(object sender, RoutedEventArgs e)
        {
            ShowSample(@"c^8=\sqrt[6]{a^2+b^2}-\pi^3");
        }

        private void Sample5(object sender, RoutedEventArgs e)
        {
            ShowSample(@"\sum_{i,j=1}^n\frac{i}{15}g_{ij}");
        }

        private void Sample6(object sender, RoutedEventArgs e)
        {
            ShowSample(@"\epsilon^k=\sqrt{\frac{\int_a^b\gamma(t)}{\sum_{i,j=1}^n\frac{i}{15}g_{ij}}}");
        }

    }
}
